package com.totobox.apps.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.totobox.apps.Const;
import com.totobox.apps.R;
import com.totobox.apps.common.MoveActivity;
import com.totobox.apps.common.TotoIFData;
import com.totobox.apps.common.Util;

public class TotoLoginActivity extends BasicActivity {
	
	public EditText txt_id;
	public EditText txt_pwd;
	public ImageButton btn_submit;
	public CheckBox chk_id;
	public CheckBox chk_auto;
	
	public ImageView img_help;
	
	private ImageView btn_find_id;
	
	private Intent intent;
	
	final private int PROGRESS_DIALOG = 0;
	final private int HELP_DIALOG = 1;
	final private int FIND_DIALOG = 2;
	private ProgressDialog progressDialog;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.toto_login);
        
        boolean isWifi = Util.isWifi(this);
        
        String login_id = Util.trim(getIntent().getStringExtra("login_id"));
        
        btn_submit = (ImageButton)findViewById(R.id.btn_submit);
        
        img_help = (ImageView)findViewById(R.id.img_help);
        
        btn_find_id = (ImageView)findViewById(R.id.btn_find_id);
        
        txt_id = (EditText)findViewById(R.id.txt_id);
        txt_id.setText(login_id);
        
        txt_pwd = (EditText)findViewById(R.id.txt_pwd);
        
        chk_id = (CheckBox)findViewById(R.id.chk_id);
        if(login_id.length()>0) chk_id.setChecked(true);
        
        chk_auto = (CheckBox)findViewById(R.id.chk_auto);

        btn_submit.setOnClickListener(this);
        img_help.setOnClickListener(this);
        btn_find_id.setOnClickListener(this);
        
        if(isWifi){
        	super.showText(this, getResources().getString(R.string.login_wifi), Toast.LENGTH_LONG);
        	
        }
        else{
        	super.showText(this, getResources().getString(R.string.login_3g) , Toast.LENGTH_LONG);
        }
    }

	@Override
	public void onClick(View v) {
		
		if(v == btn_submit){
			
			String id = txt_id.getText().toString();
			String pwd = txt_pwd.getText().toString();
			
			if(id == null || Util.trim(id).length() == 0){
				super.showText(this, "아이디를 입력하세요!");
			}
			else if(pwd == null || Util.trim(pwd).length() == 0){
				super.showText(this, "비밀번호를 입력하세요!");
				txt_pwd.setFocusable(true);
				
			}
			else{
				
				btn_submit.setFocusable(true);
				
				showDialog(PROGRESS_DIALOG);
				
				if(login(id , pwd)){
					MoveActivity go = new MoveActivity(TotoLoginActivity.this , MyTotoActivity.class);
					go.startActivity(TotoLoginActivity.this);
				}
				else{
					progressDialog.dismiss();
					TotoLoginActivity.this.removeDialog(PROGRESS_DIALOG);
					super.showText(this, "입력하신 아이디와 비밀번호가 일치하지 않습니다",Toast.LENGTH_LONG);
				}
			}
		}
		else if(v == img_help){
			showDialog(HELP_DIALOG);
		}
		else if(v == btn_find_id){
			showDialog(FIND_DIALOG);
		}
	}
    
    public boolean login(String id , String pwd){
    	
    	int result = TotoIFData.jsonLogin(id , pwd , "android");
    	
    	if(result == 0 ){
    		
    		boolean isId = chk_id.isChecked();
    		boolean isAuto = chk_auto.isChecked();
    		
    		
    		SharedPreferences pref = TotoLoginActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
        	SharedPreferences.Editor editor = pref.edit();
        	
        	if(isId){
        		editor.putString("totobox_id", id);
        	}
        	else{
        		editor.putString("totobox_id", "");
        	}
        	
        	if(isAuto) {
        		editor.putBoolean("totobox_auto", true);
        		editor.putString("totobox_pw", pwd);
        	}
        	else{
        		editor.putBoolean("totobox_auto", false);
        		editor.putString("totobox_pw", "");
        	}
        	
        	editor.commit();
    		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    @Override
	protected Dialog onCreateDialog (int id) {
    	AlertDialog.Builder builder = null;
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("로그인 중 입니다~ 잠시 기다려 주세요.");
			return progressDialog;
		case HELP_DIALOG:
			builder = new AlertDialog.Builder(this);
			builder.setMessage(this.getResources().getString(R.string.login_alert))
			       .setCancelable(false)
			       .setPositiveButton("확인", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
									 dialog.cancel();
			           }
			       }).create().show();
			
			return null;
		case FIND_DIALOG:
			builder = new AlertDialog.Builder(this);
			builder.setMessage(this.getResources().getString(R.string.find_id_alert))
			       .setCancelable(false)
			       .setPositiveButton("확인", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
									 dialog.cancel();
			           }
			       }).create().show();
			return null;
		default:
			return null;
		}
	}
    
}